package com.gta.edu.sdk.common.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
/**
 * 通用拦截, 支持跨域
 */
public class CommonInterceptor  extends HandlerInterceptorAdapter implements HandlerInterceptor {
	protected Logger logger = LoggerFactory.getLogger(getClass());
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, 
			Object handler) throws Exception {
//		Access-Control-Allow-Origin、Access-Control-Allow-Method、Access-Control-Allow-Headers
//		GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Method", "POST, OPTIONS");
		response.setHeader("Access-Control-Allow-Headers", "Origin, accept, X-Requested-With, Content-Type, sign, token, devType");
		
		response.setContentType("text/json;charset=utf-8");  
		
//		String custId = request.getParameter("custId");
//		if(!StringUtils.isBlank(custId)) {
//			for (int i = 0;i < custId.length();i++) {
//				if(Character.isUpperCase(custId.charAt(i)) == true) {
//					PrintWriter out = null;
//					out = response.getWriter();
//					out.append("{\"ret\":\"2\",\"msg\":\"请卸载后重新安装\"}");
//					return false;
//				}
//			}
//		}
		
		return true;
	}
	
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, 
			ModelAndView modelAndView) throws Exception {
	}
	
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, 
			Object handler, Exception ex) throws Exception {
	}
	
	
}
